package com.zm.openapi.filters.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @description: 修改ucs系统 Http 请求body的过滤器工厂
 * @module: com.zm.apigateway.Filters
 * @author: songxiongli
 * @date: 2020-03-13 12:30
 **/
@Slf4j
@Component
public class ModifyUcsHttpBodyGatewayFilterFactory extends AbstrModifyHttpBodyGatewayFilterFactory {
//
//    @Resource
//    private AccountFeignClient accountFeignClient;

    public ModifyUcsHttpBodyGatewayFilterFactory() {
        super();
    }

    @Override
    protected HttpBodyModifier getHttpBodyModifier() {
        return (webExchange, preBody) -> {
//            List<String> list = webExchange.getRequest().getHeaders().get(UcsShiroConstant.UCS_AUTHEN_TOKEN_NAME);
//            String token = CollectionUtils.isEmpty(list) ? StringUtils.EMPTY : list.get(0);
//            ApiResult apiResult = accountFeignClient.getUcsBaseRequestContext(token);
//            if (!apiResult.isSuccess()) {
//                throw new BusinessException(apiResult.getCode(), apiResult.getMsg());
//            }
//            UcsBaseRequest baseRequest = (UcsBaseRequest) apiResult.getData();
//            if (baseRequest != null
//                    && baseRequest.getContextAccountId() != null
//                    && baseRequest.getContextAccountId() != 0) {
//                //请求头中增加用户山下文
//                ServerHttpRequest serverHttpRequest = webExchange.getRequest().mutate()
//                        .header(UcsShiroConstant.HTTP_HEADER_UCS_USER_CONTEXT, JSON.toJSONString(baseRequest)).build();
//                webExchange.mutate().request(serverHttpRequest).build();
//            }
//            Map<String, Object> baseMap = ConvertUtil.convert(baseRequest);
//            if (MapUtils.isNotEmpty(baseMap)) {
//                preBody.putAll(baseMap);
//            }
            return preBody;
        };
    }

}
